;--------------------------------------
;
; prog1-7.asm
;
; Demonstration of MASM 6.0 simplified 
; segment and conditional assembly for
; small and large memory models and a
; demonstration of .WHILE 1 endless 
; loop and .BREAK directives
;
;

; defines here

WRITE_STRING        equ    13h
SCROLL_WINDOW       equ    06h
CLEAR_WINDOW        equ    00h
NORMAL_ATTR         equ    07h
SET_CURSOR_POSITION equ    02h

; select memory model

IF mdl EQ 3
    .Model large
ELSE
    .Model small
ENDIF  

; Create stack

    .Stack                        ; 1 K by default

; Declare data segment

IF mdl EQ 3
    .Fardata
ELSE
    .Data
ENDIF

hello      db    'Hello Chuck!',0
counter    db    0                ; loop counter

; Begin code segment

    .Code

; Start program here

    .Startup

;--------------------------------------
;
; clear the screen via bios
;

    mov    ah,SCROLL_WINDOW       ; scroll window function
    mov    al,CLEAR_WINDOW        ; used to clear window
    mov    bh,NORMAL_ATTR         ; with the normal attribute
    mov    ch,0                   ; window upper left row = 0
    mov    cl,0                   ; window upper left col = 0
    mov    dh,24                  ; window lower right row = 24
    mov    dl,79                  ; window lower right col = 79
    int    10h                    ; clear window via BIOS


;--------------------------------------
;
; Main body of program here
; where DOS interrupt 21h function
; 09h is used to print the now-
; infamous "Hello CHuck!" message
; to the screen
;

IF mdl EQ 3
    push   ds                     ; save ds register
    mov    ax,SEG hello           ; set ds register to
    mov    ds,ax                  ; far data segment of
    assume ds:SEG hello           ; hello
ENDIF

    push   ds                     ; move ds
    pop    es                     ; to es
    push   es                     ; save es
    push   bp                     ; save bp

; create loop which goes through 20 iterations

    .WHILE 1                      ; repeat in endless loop
    mov    ax,offset hello        ; hello -> bp
    mov    bp,ax
    mov    ah,WRITE_STRING        ; write string via BIOS
    mov    al,0                   ; char string & update chrsor
    mov    bh,0                   ; on video page 0
    mov    bl,7                   ; normal sttribute
    mov    cx,12                  ; string of 12 bytes
    mov    dh,counter             ; at row = counter
    mov    dl,counter             ; at column = counter
    int    10h                    ; write string bia BIOS int 10h
    inc    counter                ; increment counter by 1
    .BREAK .IF (counter==20)      ; break out of loop if counter == 20
    .ENDW                         ; end while

    pop    bp                     ; restore bp
    pop    es                     ; restore es

IF mdl EQ 3
    pop    ds                     ; restore ds
ENDIF

;--------------------------------------
;
; move the cursor to the bottom of
; the screen
;

    mov    ah,SET_CURSOR_POSITION ; move the cursor
    mov    bh,0                   ; on page 0
    mov    dh,23                  ; to row 23
    mov    dl,0                   ; column 0
    int    10h                    ; via BIOS int 10h

; End program and return to DOS

    .Exit

; End of ASSEMBLY source file

    END

;
;--------------------------------------
